﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eDoc.Controls.Device
{
    public partial class ucDeviceLogSync : System.Web.UI.UserControl
    {
        private string uuid = "";
        private DataTable dtLog = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Request.QueryString["uuid"]!=null)
            {
                uuid = Request.QueryString["uuid"];
                ltrUuid.Text = uuid;
            }
            else
            {
                Response.Redirect("/devicelist.aspx");
            }
            if(!IsPostBack)
            {
                BindData();
            }
        }

        private DataTable ReturnTable(string uuid)
        {
            DataTable dt = new DataTable();
            dt = BO.Data.Select_LogSync_by_Uuid(uuid);
            return dt;
        }

        private ArrayList ReturnEmail(DataTable dt)
        {
            ArrayList arrEmail = new ArrayList();
            for(int i =0; i< dt.Rows.Count; i++)
            {
                if(arrEmail.IndexOf(dt.Rows[i]["Email"].ToString())<0)
                {
                    arrEmail.Add(dt.Rows[i]["Email"].ToString());
                }
            }

            return arrEmail;
        }

        private void BindData()
        {
            dtLog = ReturnTable(uuid);
            repEmail.DataSource = ReturnEmail(dtLog);
            repEmail.DataBind();
        }

        protected void repEmail_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbEmail = (Label)e.Item.FindControl("lbEmail");
                lbEmail.Text = e.Item.DataItem.ToString();

                Repeater repLogSync = (Repeater)e.Item.FindControl("repLogSync");
                repLogSync.DataSource = dtLog.Select("Email='" + lbEmail.Text.Trim() + "'");
                repLogSync.DataBind();
            }
        }

        protected void repLogSync_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblContent = (Label)e.Item.FindControl("lblContent");
                //Label lblCode = (Label)e.Item.FindControl("lblCode");
                Label lblDateLog = (Label)e.Item.FindControl("lblDateLog");
                DataRow dr = (DataRow)e.Item.DataItem;
                lblContent.Text = dr[3].ToString();
                //lblCode.Text = dr[4].ToString();
                lblDateLog.Text = Convert.ToDateTime(dr[5]).ToString("dd/MM/yyyy HH:mm");
                
            }
        }
    }
}